# 任务2：酒仙网的登录，搜索并加入购物车业务的日志，报告，发送邮件，unittest框架或者pytest框架
from selenium import webdriver
from selenium.webdriver.common.by import By
import pytest,unittest,time
@pytest.fixture()
def driver():
    driver=webdriver.Edge()
    driver.get('https://www.jiuxian.com/')
    driver.maximize_window()
    yield driver

def test_login(driver):
    # # 登录
    # driver.find_element(By.XPATH,'/html/body/div[2]/div/div[1]/div/a[1]').click()
    # time.sleep(3)
    # # 输入用户名和密码
    # driver.find_element(By.XPATH,'//*[@id="userName"]').send_keys('18790225065')
    # driver.find_element(By.XPATH, '//*[@id="password"]').send_keys('duanmengna0225@')
    # time.sleep(15)
    # # 点击登录
    # driver.find_element(By.XPATH,'/html/body/div[1]/form/div/div/div/div/div[1]/div[2]/div[4]/input').click()
    # time.sleep(2)
    # # 点击确定
    # driver.find_element(By.XPATH,'/html/body/div[1]/div[4]/div[2]/div[2]/a').click()
    # time.sleep(2)
    # # 点击搜索框
    # driver.find_element(By.CLASS_NAME, 'search-form').send_keys('茅台')
    # time.sleep(10)
    # # 点击按钮
    # driver.find_element(By.XPATH,'/html/body/div[3]/div/div[2]/div[1]/form/input[2]').click()
    # time.sleep(2)
    # # 点击商品
    # driver.find_element(By.XPATH,'/html/body/div[6]/div/div[2]/div[5]/ul/li[1]/div[2]/div[1]/a/img').click()
    # time.sleep(2)
    # # 跳转
    # driver.switch_to.window(driver.window_handles[-1])
    # # 点击加入购物车
    # driver.find_element(By.XPATH,'//*[@id="addToCartForDetail"]').click()
    # time.sleep(2)

    driver.quit()
    driver=webdriver.Edge()
    driver.get('https://www.jiuxian.com/')
    driver.maximize_window()
    # 免登录
    cookie=driver.get_cookies()
    for i in cookie:
        driver.add_cookie(i)
    # 点击搜索框
    driver.find_element(By.CLASS_NAME, 'search-form').send_keys('茅台')
    time.sleep(10)
    # 点击按钮
    driver.find_element(By.XPATH,'/html/body/div[3]/div/div[2]/div[1]/form/input[2]').click()
    time.sleep(2)
    # 点击商品
    driver.find_element(By.XPATH,'/html/body/div[6]/div/div[2]/div[5]/ul/li[1]/div[2]/div[1]/a/img').click()
    time.sleep(2)
    # 跳转
    driver.switch_to.window(driver.window_handles[-1])
    # 点击加入购物车
    driver.find_element(By.XPATH,'//*[@id="addToCartForDetail"]').click()
    time.sleep(2)

if __name__ == '__main__':
    pytest.main()

